/////////////////////////////////////////////////////////////////////////////
// 2023 -
//open source under Apache License Version 2.0
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#ifndef _HDFS_LIBHDFS3_CLIENT_RAW_ERASURE_DECODER_H_
#define _HDFS_LIBHDFS3_CLIENT_RAW_ERASURE_DECODER_H_

#include <kmhdfs/client/byte_buffer.h>
#include <kmhdfs/client/erasure_coder_options.h>
#include <kmhdfs/client/byte_buffer_decoding_state.h>
#include <kmhdfs/client/ec_chunk.h>

#include <vector>
#include <deque>
#include <memory>

namespace Hdfs {
namespace Internal {

class RawErasureDecoder {
public:
    RawErasureDecoder();
    explicit RawErasureDecoder(ErasureCoderOptions & coderOptions);
    ~RawErasureDecoder() = default;

    virtual void doDecode(const std::shared_ptr<ByteBufferDecodingState> & decodingState);

    void decode(std::vector<std::shared_ptr<ECChunk>> & inputs,
                std::vector<int> & erasedIndexes,
                std::vector<std::shared_ptr<ECChunk>> & outputs);

    void decode(std::vector<std::shared_ptr<ByteBuffer>> & inputs,
                std::vector<int> & erasedIndexes,
                std::vector<std::shared_ptr<ByteBuffer>> & outputs);

    void prepareDecoding(const std::vector<std::shared_ptr<ByteBuffer>> & inputs, const std::vector<int> & erasedIndexes);

    void processErasures(const std::vector<int> & erasedIndexes);
    
    void generateDecodeMatrix(const std::vector<int> & erasedIndexes);

    int getNumDataUnits() const;
    int getNumAllUnits() const;
    bool isAllowVerboseDump() const;

    virtual void release();

private:

    // relevant to schema and won't change during decode calls
    std::vector<int8_t> encodeMatrix;

    /**
     * Below are relevant to schema and erased indexes, thus may change during
     * decode calls.
     */
    std::vector<int8_t> decodeMatrix;
    std::vector<int8_t> invertMatrix;

    /**
     * Array of input tables generated from coding coefficients previously.
     * Must be of size 32*k*rows
     */
    std::vector<int8_t> gfTables;
    std::vector<int> cachedErasedIndexes;
    std::vector<int> validIndexes;
    int numErasedDataUnits;
    std::deque<bool> erasureFlags;

    ErasureCoderOptions coderOptions;
};
    
}
}

#endif /* _HDFS_LIBHDFS3_CLIENT_RAW_ERASURE_DECODER_H_ */
